<template>
    <div class="w100p fullbg">
        <div class="_container_ clearfix pt46 opa0 trans3 postionre" :style="{'opacity':mainshow}">
            <!--导航-->
            <ChildrenRouter :routers="navs"
                            class="fl"
                            :two_id="two_id"
                            :three_id="three_id"
                            :four_id="four_id"
                            @handleClose="handleClose"
                            @handleOpen="handleOpen"
                            :ref="'childrenRouter'"
            ></ChildrenRouter>
            <!--内容-->
            <div class="w82_5p bgfff fr minh814 postionre pb120 media_w100p">
                <Title :links="links"  :isdetail="isdetail" @back="back" @menuShow="menuShow"></Title>
                <PageRight @detailTap="detailTap"
                           @currentChange="currentChange"
                           :isdetail="isdetail"
                           :dataLists="dataLists"
                           :pageCount="pageCount"
                           :isOnlyOne="isOnlyOne"
                           :status="status"
                           :currentPage="currentPage"
                           @refresh="refresh"
                            ref="pageright"
                ></PageRight>
            </div>
        </div>
    </div>
</template>

<script>
    import ChildrenRouter from './../components/ChildrenRouter'
    import Title from './../components/commons/Title'
    import PageRight from './../components/commons/PageRight'

    export default {
        name: "Details",
        components:{ChildrenRouter,Title,PageRight},
        data(){
            return{
                navs:{
                    title:'资讯中心',
                    data:''
                },
                links:{
                    one:{text:'资讯中心',link:'/information/zhibo'},
                    two:{text:'新闻直播',link:''},
                    title:'',
                    index:0,
                    four:''
                },
                one_id:0,//一级分类id
                two_id:0,//二级分类id
                three_id:0,//三级分类id
                four_id:0,
                single_id:0,//单个id
                maishow:0,
                types:'',
                index:0,
                indextwo:0,//二级index
                alltypes:null,
                isdetail:false,
                actions:'',// single -- 首页单条内容 nav -- 导航 more -- 更多
                ajaxUrl:'',
                dataLists:[{down_status:0}],//所以数据
                pageCount:0,//总页数
                isOnlyOne:false,//是否只有一条数据
                status:0 ,//0--不可以看涨 1--可以
                currentPage:1
            }
        },
        created(){
            var v = this , _path = '',
                _route = this.$route;
            v.alltypes = JSON.parse(localStorage.getItem('types')) || '';
            this.refreshLinks(this.$route);
        },
        beforeDestroy(){
            localStorage.removeItem('isdetail');
        },
        watch:{
            '$route'(to,from){
                this.refreshLinks(to);
            }
        },
        methods:{
            handleOpen(options){
                var v = this;
                v.currentPage = 1;
                if(options.method == 'one'){
                    v.two_id = options.oneid;
                    v.three_id = 0;
                    v.four_id = 0;
                    // v.index = index;
                    v.$set(v.links,'title',v.navs.data[options.index].name);
                    v.$set(v.links,'one',{text:v.navs.name,id:v.navs.term_id});
                    v.$set(v.links,'two',{text:v.navs.data[options.index].name,id:v.navs.data[options.index].term_id});
                    v.$set(v.links,'index',v.index);
                    v.$router.push({path:'/details/'+v.one_id,query:{index:v.index,two:this.two_id,taps:'more',status:options.status,v:+new Date()}})
                }else if(options.method == 'two'){
                    if(v.three_id == options.twoid){return}
                    v.two_id = options.oneid;
                    v.three_id = options.twoid;
                    v.status = options.status;
                    v.four_id = 0;
                    v.$set(v.links,'title',v.navs.data[options.index].name);
                    v.$set(v.links,'one',{text:v.navs.name,id:v.navs.term_id});
                    v.$set(v.links,'two',{text:v.navs.data[options.index].name,id:v.navs.data[options.index].term_id});
                    v.$set(v.links,'index',v.index);
                    v.action = 'more';
                    v.getInfo('','3');
                }else if(options.method == 'three'){
                    if(v.four_id == options.three ){return}
                    v.two_id = options.oneid;
                    v.three_id = options.twoid;
                    v.four_id = options.three;
                    v.status = options.status;
                    v.$set(v.links,'title',v.navs.data[options.index].name);
                    v.$set(v.links,'one',{text:v.navs.name,id:v.navs.term_id});
                    v.$set(v.links,'two',{text:v.navs.data[options.index].name,id:v.navs.data[options.index].term_id});
                    v.$set(v.links,'index',v.index);
                    v.action = 'more';
                    v.getInfo('','4');
                }

            },
            handleClose(key,keypath){
                this.rooterChange(key);
                // console.log(key,keypath)
            },
            rooterChange(key){
                var _path = this.navs.childrens[key].link;
                this.links.three = this.navs.childrens[key];
                this.active_router = this.links.three.link;
                this.$router.push(_path);
            },
            detailTap(index,title){
                this.isdetail = true;
                localStorage.setItem('isdetail',this.isdetail);
            },
            back(){
                this.isdetail = false;
                if(this.action=='single'){//从详情点击回来
                    this.$router.push({path:'/details/'+this.one_id,query:{index:0,two:this.two_id,three:this.single_id,taps:'more',status:this.status}});
                }
                localStorage.setItem('isdetail',this.isdetail);
            },
            refreshLinks(to){
                var v = this , _path = '';
                v.one_id = to.params.type || 0;
                v.action = to.query.taps || '';
                v.status = to.query.status || 0;
                v.alltypes.forEach(function (item,index) {
                    if(item.term_id == v.one_id){
                        v.index = index;
                    }
                });
                v.navs = v.alltypes[v.index];
                v.two_id = to.query.two || v.navs.data[0].term_id;
                v.single_id = to.query.three || 0;
                v.getInfo();
                v.$set(v.links,'one',{text:v.navs.name,id:v.navs.term_id});
                v.$set(v.links,'index',v.index);
                var i = 0 , item = null;
                for (i ; i < v.navs.data.length ; i ++ ){
                    item = v.navs.data[i];
                    if(item.term_id == v.two_id){
                        v.indextwo = i;
                        v.$set(v.links,'title',v.navs.data[v.indextwo].name);
                        v.$set(v.links,'two',{text:v.navs.data[v.indextwo].name,id:v.navs.data[v.indextwo].term_id});
                        return
                    }
                }
            },
            getInfo(page,status){
                var v = this ,
                    params = new FormData();
                v.mainshow = 1;
                    // console.log(v.action);
                if(v.action == 'single' ){//首页单条内容
                    this.isdetail = true;
                    localStorage.setItem('isdetail',this.isdetail);
                    this.$nextTick(function () {
                        v.$refs.pageright.getSingleDetail(v.single_id);
                    });
                    return
                }else if(v.action == 'more'){
                    this.isdetail = false;
                    v.ajaxUrl =  '/index.php?g=portal&m=index&a=get_more_news';
                    if(v.four_id > 0 ){
                        params.append('term_id',v.four_id);
                    }else if(v.three_id > 0){
                        params.append('term_id',v.three_id);
                    }else{
                        params.append('term_id',v.two_id);
                    }
                }
                if(page>0){
                    params.append('page',page);
                }
                v.$axios.post(v.ajaxUrl,params)
                    .then((res) => {
                        v.mainshow = 1;
                        res = res.data;
                        if (res.result == 1) {
                            v.dataLists = res.data;
                            v.pageCount = res.count-0;
                            if(v.dataLists.length == 1 && v.dataLists[0].down_status !=2 ){
                                v.isOnlyOne = true;
                            }else {
                                v.isOnlyOne = false;
                            }
                        }else {
                            v.isOnlyOne = false;
                            v.dataLists = [{down_status:0}];
                            res.msg && (v.$message.error(res.msg));
                        }
                    }).catch((err) => {
                        v.dataLists = [{down_status:0}];
                        v.mainshow = 1;
                        v.isOnlyOne = false;
                        v.$message.error('获取失败！');
                });

            },
            currentChange(page){
                this.getInfo(page);
            },
            refresh(){
                this.getInfo();
            },
            menuShow(){
                this.$refs.childrenRouter.menuShow()
            }
        }
    }
</script>

<style>

</style>
































